/**
 * 
 */
package com.qdairlines.controller.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qdairlines.common.ControllerUtil;
import com.qdairlines.common.Page;
import com.qdairlines.constant.BaseConsts;
import com.qdairlines.constant.DictionaryConsts;
import com.qdairlines.constant.RequestConsts;
import com.qdairlines.controller.base.BaseController;
import com.qdairlines.entity.user.Permission;
import com.qdairlines.entity.user.RolePermission;
import com.qdairlines.service.dict.DictionaryService;
import com.qdairlines.service.user.PermissionService;
import com.qdairlines.service.user.RolePermissionService;

/**
 * @author pangyannan
 * @date 2017年3月9日
 * @company QingDao Airlines
 * @description 权限控制
 */
@Controller
@RequestMapping("permission")
public class PermissionController  extends BaseController{

	@Autowired
	private PermissionService permissionService;
	
	@Autowired
	private RolePermissionService rolePermissionService;
	
	@Autowired
	private DictionaryService dictionaryService;

	/**
	 * 进入页面
	 * @return
	 */
    @RequestMapping( value = "toPage" ,produces =RequestConsts.PRODUCES)
    public String toPage(HttpServletRequest request,Model model) {
    	model.addAttribute(BaseConsts.LANGUAGE, ControllerUtil.getLanguage(request));
    	return "/user/permissionlist";
    }	

    
	/**
	 * 获取单个权限数据
	 * @param request
	 * @param permissionId
	 * @return
	 */
    @RequestMapping(value = "getPermission" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String getPermission(HttpServletRequest request,Integer permissionId) {
    	Permission  p = permissionService.getByPrimaryKey(permissionId);
     	return ControllerUtil.getJson(p);
    }
    
    /**
     * 权限列表
     * @param request
     * @param permission
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(value = "listPermissionPage",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String listPermissionPage(HttpServletRequest request,Permission permission) {
    	Page page = permissionService.getPageFromRequest(request);
    	page = permissionService.listData(permission, page);
    	
    	List<Permission> list= page.getRows();
    	dictionaryService.convertDictValueToText(list, "status", "statusName", DictionaryConsts.PERMISSION_STATUS, ControllerUtil.getLanguage(request));
    	
    	return ControllerUtil.getJson(page);
    }
    
    /**
     * 保存权限
     * @param request
     * @param dutySchedule
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "save",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String save(HttpServletRequest request,Permission permission) { 
    	Permission permissionCk1 = permissionService.getPermissionByCode(permission.getPermissionCode());
    	Permission permissionCk2 = permissionService.getPermissionByName(permission.getPermissionName());
    	
    	if(permission.getId() == null){
    		//新增判断
    		if(permissionCk1 != null || permissionCk2 != null){
    			return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    		}
    		//新增
    		permission.setCreateBy(ControllerUtil.getUser(request).getWorkcode());
    		permission.setCreateTime(new Date());
    		permission.setStatus(BaseConsts.POJO_STATUS_ABLE);
    		permissionService.saveSelective(permission);	
    	}else{
    		//修改判断
    		if(permissionCk1 != null && !permission.getId().equals(permissionCk1.getId())){
    			return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    		}
    		if(permissionCk2 != null && !permission.getId().equals(permissionCk2.getId())){
    			return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    		}
    		//修改
    		permission.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    		permission.setUpdateTime(new Date());
    		permissionService.updateByPrimaryKeySelective(permission);
    	}
    	
    	return  ControllerUtil.getJson(permission);
    }
    
    /**
     * 禁用权限
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "disable" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String disable(Integer permissionId,HttpServletRequest request){
    	RolePermission rp = new RolePermission();
    	rp.setPermissionId(permissionId);
    	List<RolePermission> rpList= rolePermissionService.listData(rp);
    	if(rpList != null && rpList.size()>0){
    		return ControllerUtil.getJson(BaseConsts.RESPONSE_MSG_ERROR);
    	}
    	
    	Permission permission = new Permission();
    	permission.setId(permissionId);
    	permission.setStatus(BaseConsts.POJO_STATUS_DISABLE);
    	permission.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    	permission.setUpdateTime(new Date());
    	permissionService.updateByPrimaryKeySelective(permission);

		return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }
    
    /**
     * 启用权限
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "able" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String able(Integer permissionId,HttpServletRequest request){
    	Permission permission = new Permission();
    	permission.setId(permissionId);
    	permission.setStatus(BaseConsts.POJO_STATUS_ABLE);
    	permission.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    	permission.setUpdateTime(new Date());
    	permissionService.updateByPrimaryKeySelective(permission);
    	
    	return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }
    
    @RequestMapping(value = "delete" ,produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String delete(@RequestParam Integer permissionId){
    	if(permissionId ==null){
    		return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_ERROR);
    	}
    	permissionService.deleteByPrimaryKey(permissionId);
		return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }
    
}
